<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.3.1.min.js"></script>
<title>Insert title here</title>
<style type="text/css">
	#body{
		margin-left: 60px;
		margin-top: 30px;
	}
	td{
		height: 60px;
		width: 200px;
	}
</style>
</head>
<body>
<div id="body">
	<table>
		<tr>
			<th>商品名称</th>
			<th>商品类型</th>
			<th>商品价格</th>
			<th>商品产地</th>
			<th>商品状态</th>
			<th>操作</th>			
		</tr>
		<c:forEach items="${Goods }" var="goods">
			<tr>
				<td align="center">${goods.goodsName }</td>
				<td align="center">${goods.goodsType }</td>
				<td align="center">${goods.goodsPrice }</td>
				<td align="center">${goods.goodsAddress }</td>
				<c:if test="${goods.goodsState == 1}"><td align="center">正常售卖</td></c:if>
				<c:if test="${goods.goodsState == 0}"><td align="center">已下架</td></c:if>
				<td align="center">
					<a href="#" onclick="goodsDown(${goods.goodsId })">下架</a>
					<a href="#" onclick="goodsUp(${goods.goodsId })">上架</a>
					<a href="#" onclick="goodsDel(${goods.goodsId })">删除</a>
				</td>
			</tr>
		</c:forEach>
	</table>
</div>
<script type="text/javascript">
	function goodsDown(goodsId){
		if(confirm("确认下架？")){
			$.ajax({
				type:"post",//设置提交方式
				url:"${pageContext.request.contextPath}/downGoods",
				async:true,//表示进行异步执行
				dataType:"json",//数据的返回类型
				data:{
					_method:"PUT",
					goodsId:goodsId
					},
				success:function(data){//data就是成功之后 Servlet返回的结果
					if(data[0] == 1){
						alert("下架成功！");
						window.open("${pageContext.request.contextPath }/GoodsDown?shopId=+${sessionScope.shop.shopId}","_self");
					}else{
						alert("下架失败");
					}
				},
				error:function(err){
					alert("操作失败");
				}
			});
		}
	}
	function goodsUp(goodsId){
		if(confirm("确认重新上架？")){
			$.ajax({
				type:"post",//设置提交方式
				url:"${pageContext.request.contextPath}/upGoods",
				async:true,//表示进行异步执行
				dataType:"json",//数据的返回类型
				data:{
					_method:"PUT",
					goodsId:goodsId
					},
				success:function(data){//data就是成功之后 Servlet返回的结果
					if(data[0] == 1){
						alert("上架成功！");
						window.open("${pageContext.request.contextPath }/GoodsDown?shopId=+${sessionScope.shop.shopId}","_self");
					}else{
						alert("上架失败");
					}
				},
				error:function(err){
					alert("操作失败");
				}
			});
		}
	}
	function goodsDel(goodsId){
		if(confirm("确认删除商品？")){
			$.ajax({
				type:"post",//设置提交方式
				url:"${pageContext.request.contextPath}/delGoods",
				async:true,//表示进行异步执行
				dataType:"json",//数据的返回类型
				data:{
					_method:"DELETE",
					goodsId:goodsId
					},
				success:function(data){//data就是成功之后 Servlet返回的结果
					if(data[0] == 1){
						alert("删除成功！");
						window.open("${pageContext.request.contextPath }/GoodsDown?shopId=+${sessionScope.shop.shopId}","_self");
					}else{
						alert("删除失败");
					}
				},
				error:function(err){
					alert("操作失败");
				}
			});
		}
	}
	function query(shopId){
		response.sendRedirect("${pageContext.request.contextPath}/GoodsDown?shopId=${sessionScope.shop.shopId}");
	}
</script>
</html>